package edu.ecut.cs.ai.search;

import java.io.File;
import edu.ecut.cs.ai.status.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.util.*;

public class AStar extends SearchAlogithm {
	public AStar() {
		MaxSteps = 362880;
	}	

	//return -1: s doesn't exist in the list; 0: s exists but isn't better than the existing status; 1: s exists and replaced the existing status because s is better. 
	@Override
	public void updateTree(Status child) {	
		int i = findStatus(child, unextendedStatuses);
		if (i >= 0) {
			if (child.betterThan(unextendedStatuses.get(i))) {
				unextendedStatuses.remove(i);
				insertStatus(child, unextendedStatuses);
			}
		} else {
			insertStatus(child, unextendedStatuses);
		}
	}	
	
	private static void insertStatus(Status s, List<Status> l) {
		Status x;
		boolean inserted = false;
		for (int i = 0; i < l.size(); i++) {
			x = l.get(i);
			if (s.betterThan(x)) {
				l.add(i, s);
				inserted = true;
				break;
			} 
		}
		
		if (!inserted) {
			l.add(s);
		}
	}
}
